import 'package:flutter/material.dart';
import 'package:citiesocial/_include.dart';
import 'package:citiesocial/widgets/main/tab_icon.dart';
import 'package:citiesocial/ui/account/my_address_add.dart';
import 'package:citiesocial/ui/account/my_address.dart';
import 'package:citiesocial/ui/account/my_cart_list.dart';
import 'package:citiesocial/ui/account/my_group_buy.dart';
import 'package:citiesocial/ui/account/my_notification.dart';
import 'package:citiesocial/ui/account/my_orders.dart';
import 'package:citiesocial/ui/account/settings.dart';
import 'package:citiesocial/ui/account/message.dart';
// import 'package:citiesocial/ui/account/';
import 'package:flutter/services.dart';

void main() {
  //强制竖屏
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(BaseMaterialApp(
    color: Configs.background,
    home: _MyApp(),
    routes: <String, WidgetBuilder>{
      '/login': (_) => Login(),
      '/forgot': (_) => Forgot(),
      '/register': (_) => Register(),
      '/address/add': (_) => MyAddressAdd(),
      '/address': (_) => MyAddress(),
      '/cart': (_) => MyCartList(),
      '/group': (_) => MyGroupBuy(),
      '/notification': (_) => MyNotification(),
      '/order': (_) => MyOrders(),
      '/settings': (_) => Settings(),
      '/message': (_) => Message(),
      // '/xx': (_) => xxx(),
    },
  ));
}

///
///主页面
///
class _MyApp extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() {
    Configs.initUserState();
    return _MyAppState();
  }
}

class _MyAppState extends BaseState<_MyApp> {
  var _currentIndex = 0;
  final _pageController = PageController();
  final _tabs = [
    CatchUp(),
    FlashBuy(),
    Catalogue(),
    Collection(),
    Account(),
  ];

  void _changeIndex(int index) {
    if (_currentIndex != index) {
      setState(() {
        _pageController.jumpToPage(index);
      });
    }
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: _currentIndex == 4
            ? AppBarUtils.account(context)
            : _currentIndex == 3 ? null : AppBarUtils.withSearch(context),
        bottomNavigationBar: SafeArea(
          child: Row(
            children: <Widget>[
              TabIcon(
                _currentIndex == 0
                    ? 'imgs/js_common_img_tab_home_h.png'
                    : 'imgs/js_common_img_tab_home_n.png',
                '逛逛',
                onTap: () => _changeIndex(0),
              ),
              TabIcon(
                _currentIndex == 1
                    ? 'imgs/js_common_img_tab_flash_h.png'
                    : 'imgs/js_common_img_tab_flash_n.png',
                '闪购',
                onTap: () => _changeIndex(1),
              ),
              TabIcon(
                _currentIndex == 2
                    ? 'imgs/js_common_img_tab_menu_h.png'
                    : 'imgs/js_common_img_tab_menu_n.png',
                '目录',
                onTap: () => _changeIndex(2),
              ),
              TabIcon(
                _currentIndex == 3
                    ? 'imgs/js_common_img_tab_favorite_h.png'
                    : 'imgs/js_common_img_tab_favorite_n.png',
                '收藏',
                onTap: () => _changeIndex(3),
              ),
              TabIcon(
                _currentIndex == 4
                    ? 'imgs/js_common_img_tab_profile_h.png'
                    : 'imgs/js_common_img_tab_profile_n.png',
                '账号',
                onTap: () => _changeIndex(4),
              ),
            ],
          ),
        ),
        backgroundColor: Colors.white,
        body: Scaffold(
          body: PageView.builder(
              physics: NeverScrollableScrollPhysics(),
              controller: _pageController,
              onPageChanged: (index) {
                if (_currentIndex != index) {
                  setState(() {
                    _currentIndex = index;
                  });
                }
              },
              itemCount: _tabs.length,
              itemBuilder: (_, index) {
                return _tabs[index];
              }),
        ),
      );
}
